package com.xxd.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.xxd.rpt3.Entity.User;

public class PasswordUtil {

	public static String createSalt() {
		return String.valueOf(Math.round(Math.random() * 100000));
	}

	public static String encryption(String password, String salt) {
		try {
			MessageDigest md = MessageDigest.getInstance("md5");
			byte[] result = md.digest((password + salt).getBytes());
			StringBuilder sBuilder = new StringBuilder();
			for (byte b : result) {
				int i = b & 0xff;// 加盐
				String mString = Integer.toHexString(i);
				if (mString.length() == 1) {
					sBuilder.append("0");
				}
				sBuilder.append(mString);
			}
			return sBuilder.toString();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return password;
	}

	public static boolean verify(User user, String password) {
		return user.getPassword().equals(PasswordUtil.encryption(password, user.getSalt()));
	}

}